#region " LICENSE "
// DRILL - Dependency Resolution Interface Layer Library
// Copyright (c) 2008-2012 Wade A. Reynolds. All rights reserved.
// http://drill.codeplex.com/
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using Drill.TestSupport.TestDomains.SimpleDomain;
using NUnit.Framework;

namespace Drill.TestHelpers
{
    /// <summary>
    /// Abstract base class used to test implementations of <see cref="IDependencyResolver"/>.
    /// </summary>
    public abstract class DependencyResolverTestFixtureBase : object
    {
        private IDependencyResolver _dependencyResolverUnderTest;

        protected abstract IDependencyResolver CreateNewDependencyResolverUnderTest();
        protected abstract void Cleanup();

        [Category("UnitTest")]
        [Test]
        public void CanGetInstanceWithTypeParameter()
        {
            try
            {
                DependencyResolverManager.Reset();
                _dependencyResolverUnderTest = CreateNewDependencyResolverUnderTest();

                Type type = typeof(ITestObjA);
                ITestObjA obj = _dependencyResolverUnderTest.GetInstance(type) as ITestObjA;

                Assert.That(obj, Is.Not.Null);
            }
            finally
            {
                Cleanup();
				DependencyResolverManager.Reset();
            }
        }

        [Category("IntegrationTest")]
        [Test]
        public void CanGetInstanceWithTypeParameterWithIntegrationTestConfig()
        {
            try
            {
                DependencyResolverManager.Reset();
                _dependencyResolverUnderTest = CreateNewDependencyResolverUnderTest();

                Type type = typeof(ITestObjA);
                ITestObjA obj = _dependencyResolverUnderTest.GetInstance(type) as ITestObjA;

                Assert.That(obj, Is.Not.Null);
            }
            finally
            {
                Cleanup();
                DependencyResolverManager.Reset();
            }
        }

        [Category("UnitTest")]
        [Test]
        public void CanGetInstanceWithTypeAndKeyParameters()
        {
            const string key = "MyObjB";
            try
            {
                DependencyResolverManager.Reset();
                _dependencyResolverUnderTest = CreateNewDependencyResolverUnderTest();

                Type type = typeof(ITestObjB);
                ITestObjB obj = _dependencyResolverUnderTest.GetInstance(type, key) as ITestObjB;

                Assert.That(obj, Is.Not.Null);
            }
            finally
            {
                Cleanup();
                DependencyResolverManager.Reset();
            }
        }

        [Category("IntegrationTest")]
        [Test]
        public void CanGetInstanceWithTypeAndKeyParametersWithIntegrationTestConfig()
        {
            const string key = "MyObjB";
            try
            {
                DependencyResolverManager.Reset();
                _dependencyResolverUnderTest = CreateNewDependencyResolverUnderTest();

                Type type = typeof(ITestObjB);
                ITestObjB obj = _dependencyResolverUnderTest.GetInstance(type, key) as ITestObjB;

                Assert.That(obj, Is.Not.Null);
            }
            finally
            {
                Cleanup();
                DependencyResolverManager.Reset();
            }
        }

        [Category("UnitTest")]
        [Test]
        public void CanGetAllInstancesWithTypeParameter()
        {
            try
            {
                DependencyResolverManager.Reset();
                _dependencyResolverUnderTest = CreateNewDependencyResolverUnderTest();

                Type type = typeof(ITestObjB);
                IEnumerable objs = _dependencyResolverUnderTest.GetAllInstances(type);

                Assert.That(objs, Is.Not.Null);

                foreach (object obj in objs)
                {
                    ITestObjB testObjB = obj as ITestObjB;
                    Assert.That(testObjB, Is.Not.Null);
                }
            }
            finally
            {
                Cleanup();
                DependencyResolverManager.Reset();
            }
        }

        [Category("IntegrationTest")]
        [Test]
        public void CanGetAllInstancesWithTypeParameterWithIntegrationTestConfig()
        {
            try
            {
                DependencyResolverManager.Reset();
                _dependencyResolverUnderTest = CreateNewDependencyResolverUnderTest();

                Type type = typeof(ITestObjB);
                IEnumerable objs = _dependencyResolverUnderTest.GetAllInstances(type);

                Assert.That(objs, Is.Not.Null);

                foreach (object obj in objs)
                {
                    ITestObjB testObjB = obj as ITestObjB;
                    Assert.That(testObjB, Is.Not.Null);
                }
            }
            finally
            {
                Cleanup();
                DependencyResolverManager.Reset();
            }
        }

        [Category("UnitTest")]
        [Test]
        public void CanGetInstanceGeneric()
        {
            try
            {
                DependencyResolverManager.Reset();
                _dependencyResolverUnderTest = CreateNewDependencyResolverUnderTest();

                ITestObjA obj = _dependencyResolverUnderTest.GetInstance<ITestObjA>();

                Assert.That(obj, Is.Not.Null);
            }
            finally
            {
                Cleanup();
                DependencyResolverManager.Reset();
            }
        }

        [Category("IntegrationTest")]
        [Test]
        public void CanGetInstanceGenericWithIntegrationTestConfig()
        {
            try
            {
                DependencyResolverManager.Reset();
                _dependencyResolverUnderTest = CreateNewDependencyResolverUnderTest();

                ITestObjA obj = _dependencyResolverUnderTest.GetInstance<ITestObjA>();

                Assert.That(obj, Is.Not.Null);
            }
            finally
            {
                Cleanup();
                DependencyResolverManager.Reset();
            }
        }

        [Category("UnitTest")]
        [Test]
        public void CanGetInstanceGenericWithKeyParameter()
        {
            const string key = "MyObjB";
            try
            {
                DependencyResolverManager.Reset();
                _dependencyResolverUnderTest = CreateNewDependencyResolverUnderTest();

                ITestObjB obj = _dependencyResolverUnderTest.GetInstance<ITestObjB>(key);

                Assert.That(obj, Is.Not.Null);
            }
            finally
            {
                Cleanup();
                DependencyResolverManager.Reset();
            }
        }

        [Category("IntegrationTest")]
        [Test]
        public void CanGetInstanceGenericWithKeyParameterWithIntegrationTestConfig()
        {
            const string key = "MyObjB";
            try
            {
                DependencyResolverManager.Reset();
                _dependencyResolverUnderTest = CreateNewDependencyResolverUnderTest();

                ITestObjB obj = _dependencyResolverUnderTest.GetInstance<ITestObjB>(key);

                Assert.That(obj, Is.Not.Null);
            }
            finally
            {
                Cleanup();
                DependencyResolverManager.Reset();
            }
        }

        [Category("UnitTest")]
        [Test]
        public void CanGetAllInstancesGeneric()
        {
            try
            {
                DependencyResolverManager.Reset();
                _dependencyResolverUnderTest = CreateNewDependencyResolverUnderTest();

                IEnumerable<ITestObjB> objs = _dependencyResolverUnderTest.GetAllInstances<ITestObjB>();

                Assert.That(objs, Is.Not.Null);

                foreach (ITestObjB obj in objs)
                {
                    Assert.That(obj, Is.Not.Null);
                }
            }
            finally
            {
                Cleanup();
                DependencyResolverManager.Reset();
            }
        }

        [Category("IntegrationTest")]
        [Test]
        public void CanGetAllInstancesGenericWithIntegrationTestConfig()
        {
            try
            {
                DependencyResolverManager.Reset();
                _dependencyResolverUnderTest = CreateNewDependencyResolverUnderTest();

                IEnumerable<ITestObjB> objs = _dependencyResolverUnderTest.GetAllInstances<ITestObjB>();

                Assert.That(objs, Is.Not.Null);

                foreach (ITestObjB obj in objs)
                {
                    Assert.That(obj, Is.Not.Null);
                }
            }
            finally
            {
                Cleanup();
                DependencyResolverManager.Reset();
            }
        }
    }
}